// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

    openid: '',
    // 当前所在滑块的index

    items: [],

    goodsId: [],

    data: '',
  },


  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onShow: function () {

    let that = this;
    const db = wx.cloud.database()
    wx.cloud.callFunction({
      name: "login",
      data: {},

      success: res => {
        let openid = res.result.openid;

        db.collection('card').where({
          _openid: openid // 填入当前用户 openid
        }).get({
          success: function (res) {
            that.setData({
              items: res.data
            })
          }
        })
      },


    })


    wx.cloud.callFunction({
      name: "login",

      success: res => {
        let openid = res.result.openid;

        db.collection('user').where({
          _openid: openid // 填入当前用户 openid
        }).get({
          success: function (res) {
            that.setData({
              image: res.data[0].image
            })
          }
        })
      },

      fail: res => {

      }
    })

  },

  onGo: function () {
    wx.navigateTo({
      url: '../fill/fill',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  onEnter: function (e) {

    wx.navigateTo({
      url: '../card/card?id=' + e.currentTarget.dataset.id,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

 

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
debugger
  },

 

  // 卡片点击
  onCardItemTap: function (e) {

    let i = e.currentTarget.dataset.i;
    let cardInfo = this.data.items[i];

    // 复制卡号
    wx.setClipboardData({
      data: cardInfo.number,
      success: result => {
        wx.navigateBack({
          delta: 1,
        })
      },
      fail: () => { },
      complete: () => { }
    });
  },

  // 复制功能
  onCopy: function (e) {
    let i = e.currentTarget.dataset.i;
    let cardInfo = this.data.items[i];

    wx.setClipboardData({
      data: cardInfo.number,
      success: result => {

      },
      fail: () => { },
      complete: () => { }
    });
  }
})